import { ConfigEnv, defineConfig, loadEnv, UserConfigExport } from 'vite'
import { resolve } from 'path'
import react from '@vitejs/plugin-react'
import UnoCSS from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'

// https://vitejs.dev/config/
export default ({ mode }: ConfigEnv): UserConfigExport => {
  const root = process.cwd()
  const env = loadEnv(mode, root)
  const { VITE_BASE_URL } = env

  process.env = { ...process.env, ...loadEnv(mode, root) }

  return defineConfig({
    base: VITE_BASE_URL,
    define: {
      'process.env': {
        ...process.env,
        REACT_APP_API_URL: 'https://www.fastmock.site/mock/8a027582c169e035aa9cd404b3dbc06f/api',
      },
    },
    resolve: {
      alias: {
        '@': resolve(__dirname, './src'),
      },
    },
    plugins: [
      react(),
      UnoCSS(),
      AutoImport({
        imports: ['react', 'react-router-dom'],
        // dirs: ['src/components/*'],
      }),
    ],
  })
}
